<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
<script type="text/javascript" src='jquery-1.7.2.js'></script>
<script type="text/javascript">
function Round(n, pos) {
	  var digits = Math.pow(10, pos);
	  var sign = 1;
	  if (n < 0) {
	   sign = -1;
	  }
	  n = n * sign;
	  var num = Math.round(n * digits) / digits;
	  num = num * sign;
	  return num.toFixed(pos);
	 }
$(document).ready(function(){
	var flag = false;
	$('#operation').keypress(function(ev){
		if (ev.which === 13){
			$('#OPstart').click();
		    return false;
		}
	});
	$(".OPBtn").hide();
	$("#realTimeUpdate").hide();
	$("#OPstart").click(function(){
		var qty = parseInt($("#quantity").val());
		var orinQty = parseInt($("#orinQty").val());
		if(isNaN(qty)){
			alert("Quantity는 숫자입력만 가능합니다");
			return false;
		}
		if(qty>orinQty){
			alert("Overflow");
			return false;
		} 		
		$("#PCSet").html("<img width='30px' src='img/animated-arrow.gif'>");
		$("#OP1Ing").html("<img width='30px' src='img/animated-arrow.gif'>");
		$("#OP2Ing").html("<img width='30px' src='img/animated-arrow.gif'>");
		$("#OP3Ing").html("<img width='30px' src='img/animated-arrow.gif'>");
		$("#selectTable").html(selectedInfo()); 
		$("#realTimeUpdate").trigger("click");
		$.ajax({
			type:"post",
			url:"process.do?command=operation",
			data:"work_order_id="+$("#work_order_id").text()+"&serial_no_start="+$("#serial_no_start").text()+"&quantity="+$("#quantity").text(),
			dataType:"json",
			success:function(data){
				alert("공정완료");
				flag=true;
			}
		});
	});
	$("#realTimeUpdate").click(function(){
		if(flag){			
			return false;
		}				
		$.ajax({  
				url:"process.do?command=realTimeUpdate",
				data:"work_order_id="+$("#work_order_id").text()+"&serial_no_start="+$("#serial_no_start").text()+"&quantity="+$("#quantity").text(),
				dataType:"json", 
				success : function(data){
					resultView(data);
					var total = parseInt(data.info[3].success)+parseInt(data.info[3].fail)+parseInt(data.info[2].fail)+parseInt(data.info[1].fail)+parseInt(data.info[0].fail);
					if(total==$("#quantity").text()){
						$("#PCSet").html("");
						$("#OP1Ing").html("");
						$("#OP2Ing").html("");
						$("#OP3Ing").html("");
						return false;
					}
					$("#realTimeUpdate").trigger("click");
				},
				error : function(){
					alert('error');
				}
			});
		}); // realTimeUpdate
	$("#work_order_id").change(function(){
		$.ajax({
			type:"post",
			url:"process.do?",
			data:"command=getWoQty&work_order_id="+$(this).val(),
			dataType:"json",
			success:function(data){
				var amt = "<input type='text' id='orinQty' value='"+data.info.qty+"' readonly='readonly'>";
				var start_no = "<input type='text' id='serial_no_start' name='serial_no_start' value='"+data.info.start_no+"'readonly='readonly'>";
				$("#totalQty").html(amt);
				$("#start_no").html(start_no);
			}
		});
	});//WO change	
});
function selectedInfo(){
	var info="";
	info+="<table border='1' cellspacing='0' width='100%'>";
	info+="<tr><Td><b>Work Order</b></Td><td><b>Quantity</b></td><td><b>Start_No</b></td><td><b>Progress Status</b></td></tr>";
	info+="<tr><td><span id='work_order_id' name='work_order_id'>"+$("#work_order_id").val()+"</span></td><td><span id='quantity' name='quantity'>"+$("#quantity").val()+"</span></td><Td><span id='serial_no_start' name='serial_no_start'>"+$("#serial_no_start").val()+"</span></Td><td><span id='pRate'></span</td></tr>";
	info+="</table>";
	return info;
}

function resultView(data){
	var op1_dno="";var op2_dno="";var op3_dno="";var op4_dno="";
	var quantity = parseInt(data.info[3].success)+parseInt(data.info[3].fail)+parseInt(data.info[2].fail)+parseInt(data.info[1].fail)+parseInt(data.info[0].fail);
	var total = $("#quantity").text();
	$("#OP1success").html(data.info[0].success);
	$("#OP1fail").html(data.info[0].fail);
	$.each(data.info[0].defect_id_list,function(index,value){
		op1_dno +="<a href='#' class='trackingSN'>"+value+"</a><br>";
	});
	
	$("#OP1failSerial").html(op1_dno);
	$("#OP2success").html(data.info[1].success);
	$("#OP2fail").html(data.info[1].fail);
	$.each(data.info[1].defect_id_list,function(index,value){
		op2_dno +="<a href='#' class='trackingSN'>"+value+"</a><br>";
	});
	$("#OP2failSerial").html(op2_dno);
	$("#OP3success").html(data.info[2].success);
	$("#OP3fail").html(data.info[2].fail);
	$.each(data.info[2].defect_id_list,function(index,value){
		op3_dno +="<a href='#' class='trackingSN'>"+value+"</a><br>";
	});
	$("#OP3failSerial").html(op3_dno);
	$("#OP4success").html(data.info[3].success);
	$("#OP4fail").html(data.info[3].fail);	
	$.each(data.info[3].defect_id_list,function(index,value){
		op4_dno +="<a href='#' class='trackingSN'>"+value+"</a><br>";
	});
	$("#OP4failSerial").html(op4_dno);
	$(".trackingSN").on("click",function(){
		window.open("process.do?command=trackingSN&serial_no="+$(this).text(),"", "resizable=no,toolbar=no,height=300px, width=600px"); 
	});
	var pRate = Round(quantity/total*100,2); 
	$("#pRate").html(pRate+"%");	
}

</script>
<hr>
<B>RB ICESCREAM MAKING PROCESS</B>
<hr>
<form id="operation">
<input type="hidden" name="command" value="operation" >
<span id="selectTable">
<table border="1"  cellspacing="0"  width="100%
">
	<tr>
		<td width="25%">Select WorkOrder : </td><td>
			<select name="work_order_id" id="work_order_id">
				<option value=""> - - - - - - </option>
					<c:forEach items="${requestScope.list}" var="WO">
						<option value="${WO.WORK_ORDER_ID}">${WO.WORK_ORDER_ID}</option>
				</c:forEach>
			</select></td>
		<td >Start_Serial_no : </td>
		<td width="20%"><span id="start_no"></span></td>	
		<td rowspan="2"><input type="button"  id="OPstart" value="Start" style="height: 40px; width: 70px"></td>	
	</tr>
	<tr>
		<td >Selected WorkOorder's Quantity :</td>
		<td width="20%"> <span id="totalQty"></span></td>	
		<td>Quantity :</td><td><input type="text" name="quantity" id="quantity"></td>
	</tr>
</table>
</span>
</form>
<input type="button" id="realTimeUpdate" value="업데이트">
<hr>

<table border='1' cellspacing='0' width="100%">
	<tr>
		<td><B>Operation</b></td><td><img width="150px" src="img/op1.jpg"></td><td width="30px" rowspan="4"><span id="OP1Ing"></span></td><td><img width="150px" src="img/op2.jpg"></td><td width="30px" rowspan="4"><span id="OP2Ing"></span></td><td><img width="150px" src="img/op3.jpg"></td><td width="30px" rowspan="4"><span id="OP3Ing"></span></td><td><img width="150px" src="img/op4.jpg"></td>
	</tr>
	<tr height="30px">
		<td><b>Pass</b></td><td ><span id="OP1success"></span></td><td><span id="OP2success"></span></td><td><span id="OP3success"></span></td><td><span id="OP4success"></span></td>
	</tr>
	<tr height="30px">
		<td><b>Fail</b></td><td><span id="OP1fail"></span></td><td><span id="OP2fail"></span></td><td><span id="OP3fail"></span></td><td><span id="OP4fail"></span></td>
	</tr>
	<tr height="70px">
		<td><b>Defect</b></td><td><span id="OP1failSerial"></span></td><td><span id="OP2failSerial"></span></td><td><span id="OP3failSerial"></span></td><td><span id="OP4failSerial"></span></td>
	</tr>
</table>



